package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallAdmin;
import com.cskaoyan.bean.CskaoyanmallRole;
import com.cskaoyan.bean.bo.PermissionBo;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.config.Logging;
import com.cskaoyan.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Fantasylkh
 */
@RestController
public class SystemController {
    @Autowired
    SystemService systemService;

    @RequestMapping("/admin/dashboard")
    public BaseRespVo dashboard() {
        TotalVo totalVo = systemService.selectTotalNum();
        return BaseRespVo.success(totalVo);

    }


    @RequestMapping("/admin/role/options")
    public BaseRespVo roleOptions() {
        List<RoleOptionsVo> roleOptions = systemService.roleOptions();
        return BaseRespVo.success(roleOptions);
    }

    @RequiresPermissions("admin:admin:list")
    @RequestMapping("/admin/admin/list")
    public BaseRespVo adminList(int page, int limit, String username, String sort, String order) {
        AdminListVo adminListVo = systemService.adminList(page, limit, username, sort, order);
        return BaseRespVo.success(adminListVo);
    }

    @RequiresPermissions("admin:admin:create")
    @Logging("创建管理员")
    @RequestMapping("/admin/admin/create")
    public BaseRespVo adminCreate(@RequestBody CskaoyanmallAdmin cskaoyanmallAdmin) {
        if (cskaoyanmallAdmin.getUsername() == null || "".equals(cskaoyanmallAdmin.getUsername())) {
            return BaseRespVo.fail(601);
        }
        int effect = systemService.selectName(cskaoyanmallAdmin);
        if (effect > 0) {
            return BaseRespVo.fail(602);
        }
        CskaoyanmallAdmin adminVo = systemService.adminCreate(cskaoyanmallAdmin);
        if (adminVo == null) {
            return BaseRespVo.fail(601);
        }
        return BaseRespVo.success(adminVo);
    }

    @RequiresPermissions("admin:admin:delete")
    @Logging("删除管理员")
    @RequestMapping("/admin/admin/delete")
    public BaseRespVo delete(@RequestBody CskaoyanmallAdmin admin) {
        int effect = systemService.deleteAdmin(admin);
        if (effect != 0) {
            return BaseRespVo.success();
        }
        return BaseRespVo.fail(0);
    }

    @RequiresPermissions("admin:admin:update")
    @Logging("更新管理员")
    @RequestMapping("/admin/admin/update")
    public BaseRespVo update(@RequestBody CskaoyanmallAdmin admin) {
        CskaoyanmallAdmin updateAdmin = systemService.updateAdmin(admin);
        if (updateAdmin == null) {
            return BaseRespVo.fail(0);
        }
        return BaseRespVo.success(updateAdmin);
    }

    @RequiresPermissions("admin:role:list")
    @RequestMapping("/admin/role/list")
    public BaseRespVo roleList(int page, int limit, String name, String sort, String order) {
        RoleListVo roleListVo = systemService.roleList(page, limit, name, sort, order);
        return BaseRespVo.success(roleListVo);
    }

    @RequiresPermissions("admin:role:create")
    @RequestMapping("/admin/role/create")
    public BaseRespVo roleCreate(@RequestBody CskaoyanmallRole role) {
        CskaoyanmallRole roleVo = systemService.roleCreate(role);
        if (roleVo == null) {
            return BaseRespVo.fail(640);
        }
        return BaseRespVo.success(roleVo);
    }

    @RequiresPermissions("admin:role:update")
    @RequestMapping("/admin/role/update")
    public BaseRespVo roleUpdate(@RequestBody CskaoyanmallRole role) {
        int effect = systemService.roleUpdate(role);
        if (effect == 0) {
            return BaseRespVo.fail(-1);
        }
        return BaseRespVo.success();
    }

    @RequiresPermissions("admin:role:delete")
    @RequestMapping("admin/role/delete")
    public BaseRespVo roleDelete(@RequestBody CskaoyanmallRole role) {
        int effect = systemService.roleDelete(role);
        if (effect == 0) {
            return BaseRespVo.fail(642);
        }
        return BaseRespVo.success();
    }

    @RequiresPermissions("admin:log:list")
    @RequestMapping("/admin/log/list")
    public BaseRespVo logList(int page, int limit, String name, String sort, String order) {
        LogListVo logListVo = systemService.logList(page, limit, name, sort, order);
        return BaseRespVo.success(logListVo);
    }

    @RequiresPermissions("admin:role:permission:get")
    @RequestMapping(value = "/admin/role/permissions", method = RequestMethod.GET)
    public BaseRespVo rolePermissions(int roleId) {
        RolePermissionsVo vo = systemService.rolePermissions(roleId);
        return BaseRespVo.success(vo);
    }

    @RequiresPermissions("admin:role:permission:update")
    @RequestMapping(value = "/admin/role/permissions", method = RequestMethod.POST)
    public BaseRespVo permissions(@RequestBody PermissionBo bo) {
        systemService.permissions(bo);
        return BaseRespVo.success();
    }

}
